package com.ruoyi.workflow.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.workflow.domain.AppointmentInfo;
import com.ruoyi.workflow.domain.Statement;
import com.ruoyi.workflow.mapper.AppointmentInfoMapper;
import com.ruoyi.workflow.mapper.StatementMapper;
import com.ruoyi.workflow.service.IAppointmentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.ruoyi.workflow.constant.AppointmentConstant.*;
import static com.ruoyi.workflow.constant.StatementConstant.APPOINTMENT_STATUS_YES;
import static com.ruoyi.workflow.constant.StatementConstant.STATEMENT_STATUS_CONSUMPTION;

/**
 * 预约信息Service业务层处理
 *
 * @author xiaodu
 * @date 2023-07-21
 */
@Service
public class AppointmentInfoServiceImpl extends ServiceImpl<AppointmentInfoMapper, AppointmentInfo> implements IAppointmentInfoService {
    @Autowired
    private AppointmentInfoMapper appointmentInfoMapper;

    @Autowired
    private StatementMapper statementMapper;

    /**
     * 查询预约信息
     *
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public AppointmentInfo selectAppointmentInfoById(Long id) {
        return appointmentInfoMapper.selectAppointmentInfoById(id);
    }

    /**
     * 查询预约信息列表
     *
     * @param appointmentInfo 预约信息
     * @return 预约信息
     */
    @Override
    public List<AppointmentInfo> selectAppointmentInfoList(AppointmentInfo appointmentInfo) {
        return appointmentInfoMapper.selectAppointmentInfoList(appointmentInfo);
    }

    /**
     * 新增预约信息
     *
     * @param appointmentInfo 预约信息
     * @return 结果
     */
    @Override
    public int insertAppointmentInfo(AppointmentInfo appointmentInfo) {
        return appointmentInfoMapper.insertAppointmentInfo(appointmentInfo);
    }

    /**
     * 修改预约信息
     *
     * @param appointmentInfo 预约信息
     * @return 结果
     */
    @Override
    public int updateAppointmentInfo(AppointmentInfo appointmentInfo) {
        return appointmentInfoMapper.updateAppointmentInfo(appointmentInfo);
    }

    /**
     * 批量删除预约信息
     *
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteAppointmentInfoByIds(Long[] ids) {
        return appointmentInfoMapper.deleteAppointmentInfoByIds(ids);
    }

    /**
     * 删除预约信息信息
     *
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteAppointmentInfoById(Long id) {
        return appointmentInfoMapper.deleteAppointmentInfoById(id);
    }

    /**
     * 已到店
     *
     * @param id 预约信息ID
     */
    @Override
    public void arrival(Long id) {
        //根据id查询当前状态
        AppointmentInfo appointmentInfo = appointmentInfoMapper.selectAppointmentInfoById(id);
        //如果当前状态不是预约中。则不可以已到店，即 抛出异常
        if (!CMI_STATUS_APPOINTMENT.equals(appointmentInfo.getStatus())) {
            throw new ServiceException("【操作违规】当前状态不是预约中，不可已到店");
        }
        //如果当前是预约中，则把类型改为 已到店 CMI_STATUS_ARRIVAL，并且设置到店时间
        super.update(new LambdaUpdateWrapper<AppointmentInfo>()
                .set(AppointmentInfo::getStatus, CMI_STATUS_ARRIVAL)
                .set(AppointmentInfo::getArrivalTime, new Date())
                .eq(AppointmentInfo::getId, id));
    }

    /**
     * 取消预约
     * @param id 预约信息ID
     */
    @Override
    public void cancel(Long id) {
        //根据id查询当前状态
        AppointmentInfo appointmentInfo = appointmentInfoMapper.selectAppointmentInfoById(id);
        //如果当前状态不是预约中。则不可以取消预约，即 抛出异常
        if (!CMI_STATUS_APPOINTMENT.equals(appointmentInfo.getStatus())) {
            throw new ServiceException("【操作违规】当前状态不是预约中，不能取消预约");
        }
        //如果当前是预约中，则把类型改为 取消预约 CMI_STATUS_CANCEL
        super.update(new LambdaUpdateWrapper<AppointmentInfo>()
                .set(AppointmentInfo::getStatus,CMI_STATUS_CANCEL)
                .eq(AppointmentInfo::getId, id));
    }

    /**
     * 生成结算单
     * @param id 预约信息的ID
     * return 返回生成结算单的ID
     */
    @Transactional
    @Override
    public void generateStatement(Long id) {
        /**
         * 生成结算单：
         * 需求：判断当前状态，如果是已到店，则生成结算单。把状态改为 结算单生成 后 添加到结算单表中[预约用户：是，结算状态：消费中]
         *                  如果是已支付，则查看结算单
         *      则返回当前生成结算单的数据，然后跳转渲染到结算单明细页。
         */
        AppointmentInfo appointmentInfo = appointmentInfoMapper.selectAppointmentInfoById(id);
        //1.判断当前状态如果是已到店
        if (CMI_STATUS_ARRIVAL.equals(appointmentInfo.getStatus())) {
            //2.把预约信息表中的状态 改为  结算单生成
            super.update(new LambdaUpdateWrapper<AppointmentInfo>()
                    .set(AppointmentInfo::getStatus, CMI_STATUS_SETTLEMENT_DOC)
                    .eq(AppointmentInfo::getId, id)
            );
            //3.添加到结算单表中 [预约用户：是，结算状态：消费中]
            Statement statement = new Statement();
            BeanUtils.copyProperties(appointmentInfo, statement);
            statement.setId(null);
            statement.setAppointmentInfoId(appointmentInfo.getId());
            statement.setAppointmentUserStatus(APPOINTMENT_STATUS_YES);
            statement.setPayStatus(STATEMENT_STATUS_CONSUMPTION);
            statement.setCreateTime(new Date());
            statementMapper.insertStatement(statement);
        }
    }


}
